package com.lostreseditores.sabersai.dataaccess.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;

import com.lostreseditores.sabersai.dataaccess.sessionFactory.HibernateSessionFactory;
import com.lostreseditores.sabersai.model.Calificaperiodo;


public class CalificaperiodoDAO implements ICalificaperiodoDAO {
    private static final Log log = LogFactory.getLog(CalificaperiodoDAO.class);

    // property constants
    //public static final Integer  ASIGIDN = "asigidn";
    public static final String ASIGIDN = "asigidn";

    //public static final Integer  CALIFIPERIODOIDN = "califiperiodoidn";
    public static final String CALIFIPERIODOIDN = "califiperiodoidn";

    //public static final Integer  CALIXESTUIDN = "calixestuidn";
    public static final String CALIXESTUIDN = "calixestuidn";

    //public static final Long  PUNTAJE = "puntaje";
    public static final String PUNTAJE = "puntaje";

    private Session getSession() {
        return HibernateSessionFactory.getSession();
    }

    /**
    *
    * @param Instance
    *            Calificaperiodo Instance to persist
    * @throws RuntimeException
    *             when the operation fails
    */
    public void save(Calificaperiodo instance) {
        log.debug("saving Calificaperiodo instance");

        try {
            getSession().save(instance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    /**
    * @param Instance
    *            Calificaperiodo Instance to delete
    * @throws RuntimeException
    *             when the operation fails
    */
    public void delete(Calificaperiodo instance) {
        log.debug("deleting Calificaperiodo instance");

        try {
            getSession().delete(instance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    /**
    *
    * @param Instance
    *            Calificaperiodo Instance to update
    * @throws RuntimeException
    *             when the operation fails
    */
    public void update(Calificaperiodo instance) {
        log.debug("updating Calificaperiodo instance");

        try {
            getSession().update(instance);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }

    public Calificaperiodo findById(Integer id) {
        log.debug("finding Calificaperiodo instance with id: " + id);

        try {
            Calificaperiodo instance = (Calificaperiodo) getSession()
                                                             .get(Calificaperiodo.class,
                    id);

            return instance;
        } catch (RuntimeException re) {
            log.error("finding Calificaperiodo failed", re);
            throw re;
        }
    }

    public List<Calificaperiodo> findByExample(Calificaperiodo instance) {
        log.debug("finding Calificaperiodo instance by example");

        try {
            List results = getSession().createCriteria("Calificaperiodo")
                               .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: " +
                results.size());

            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    /**
    * Find all  Calificaperiodo entities with a specific property value.
    *
    * @param value
    *            the property value to match
    * @param propertyName
    *            the property to search in the instance
    * @return List< Calificaperiodo> found by query
        */
    public List<Calificaperiodo> findByProperty(String propertyName,
        Object value) {
        log.debug("finding Calificaperiodo instance with property: " +
            propertyName + ", value: " + value);

        try {
            String queryString = "from Calificaperiodo as model where model." +
                propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<Calificaperiodo> findByAsigidn(Object asigidn) {
        return findByProperty(ASIGIDN, asigidn);
    }

    public List<Calificaperiodo> findByCalifiperiodoidn(Object califiperiodoidn) {
        return findByProperty(CALIFIPERIODOIDN, califiperiodoidn);
    }

    public List<Calificaperiodo> findByCalixestuidn(Object calixestuidn) {
        return findByProperty(CALIXESTUIDN, calixestuidn);
    }

    public List<Calificaperiodo> findByPuntaje(Object puntaje) {
        return findByProperty(PUNTAJE, puntaje);
    }

    /**
    * Find all Calificaperiodo entities.
    *
    * @return List<Calificaperiodo> all Calificaperiodo instances
    */
    public List<Calificaperiodo> findAll() {
        log.debug("finding all Calificaperiodo instances");

        try {
            String queryString = "from Calificaperiodo";
            Query queryObject = getSession().createQuery(queryString);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public List<Calificaperiodo> findByCriteria(String whereCondition) {
        log.debug("finding Calificaperiodo " + whereCondition);

        try {
            String where = ((whereCondition == null) ||
                (whereCondition.length() == 0)) ? "" : ("where " +
                whereCondition);
            final String queryString = "select model from Calificaperiodo model " +
                where;
            Query query = getSession().createQuery(queryString);
            List<Calificaperiodo> entitiesList = query.list();

            return entitiesList;
        } catch (RuntimeException re) {
            log.error("find By Criteria in Calificaperiodo failed", re);
            throw re;
        }
    }

    public List<Calificaperiodo> findPageCalificaperiodo(
        String sortColumnName, boolean sortAscending, int startRow,
        int maxResults) {
        log.debug("finding Calificaperiodo findPageCalificaperiodo");

        if ((sortColumnName != null) && (sortColumnName.length() > 0)) {
            try {
                String queryString = "select model from Calificaperiodo model order by model." +
                    sortColumnName + " " + (sortAscending ? "asc" : "desc");

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        } else {
            try {
                String queryString = "select model from Calificaperiodo model";

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        }
    }

    public Long findTotalNumberCalificaperiodo() {
        log.debug("finding Calificaperiodo count");

        try {
            String queryString = "select count(*) from Calificaperiodo model";
            Query queryObject = getSession().createQuery(queryString);

            return (Long) queryObject.list().get(0);
        } catch (RuntimeException re) {
            throw re;
        }
    }
}
